package com.example.parsing.unit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author liutongqiang
 * @version 0.0.1-SNAPSHOT
 * @date 2019/3/13
 */
public class NoGenerator {

    private static Logger log = LoggerFactory.getLogger(NoGenerator.class);
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * @return 32λUUID
     */
    public synchronized static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * @return long
     */
    public synchronized static long getDate() {
        Date date = new Date();
        return date.getTime();
    }


    public synchronized static String getTime() {
        return simpleDateFormat.format(new Date());
    }

    public static Long getDate(String time) {
        try {
            Date date = simpleDateFormat.parse(time);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            log.error("自动生成编号工具类,根据时间获取对应时间戳异常:" + e.getMessage());
            return NoGenerator.getDate();
        }

    }

    public static Long getlockingCarDate() {
        try {
            String time = simpleDateFormat.format(new Date());
            int year = Integer.parseInt(time.substring(0, 4));
            int newyear = year + 100;
            time.replaceFirst(year + "", newyear + "");
            Date date = simpleDateFormat.parse(time);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("自动生成编号工具类,异常:" + e.getMessage());
            return null;
        }

    }

    public static Long getInfluxTime(String time) {
        Long t = getDate(time);
        int len = t.toString().length();
        len = 19 - len;
        switch (len) {
            case 1:
                t = t * 10;
                break;
            case 2:
                t = t * 100;
                break;
            case 3:
                t = t * 1000;
                break;
            case 4:
                t = t * 10000;
                break;
            case 5:
                t = t * 100000;
                break;
            case 6:
                t = t * 1000000;
                break;
            case 7:
                t = t * 10000000;
                break;
            case 8:
                t = t * 100000000;
                break;
            case 9:
                t = t * 1000000000;
                break;
        }
        return t;
    }

}
